package com.order.hit.crossover.platform.common.utils;

import com.order.hit.crossover.platform.common.bean.order.Quotation;
import com.order.hit.crossover.platform.common.bean.Good;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ValueStrategy
{

    //  计算两个地址之间的直线距离（单位：m）
    public static double getDistance(String address_1, String address_2) {

        double lat_1 = (double) GetLatAndLngByBaidu.getLngAndLat(address_1).get("lat");
        double lng_1 = (double)GetLatAndLngByBaidu.getLngAndLat(address_1).get("lng");

        double lat_2 = (double)GetLatAndLngByBaidu.getLngAndLat(address_2).get("lat");
        double lng_2 = (double)GetLatAndLngByBaidu.getLngAndLat(address_2).get("lng");

        double distance = PointToDistance.getDistanceFromTwoPoints(lat_1,lng_1,lat_2,lng_2);

        return distance;
    }

    //  最低价格策略：按照报价单给出的价格由低到高排序
    public static List<Quotation> lowPrice(List<Quotation> quotationList) {

        List<Quotation> quotations = new ArrayList<>();

        int size = quotationList.size();

        //  存储下标的数组
        int a[] = new int [size];
        for(int i=0;i<size;i++) {
            a[i] = i;
        }

        //  由小到大排序,记录排序后的下标
        for(int i=0;i<size;i++) {
            for(int j=0;j<size;j++) {
                if(quotationList.get(a[i]).getPrice() > quotationList.get(a[j]).getPrice()) {
                    int k = a[j];
                    a[j] = a[i];
                    a[i] = k;
                }
            }
        }

        //  按照下标重新排列
        for(int i=size-1;i>=0;i--) {
            quotations.add(quotationList.get(a[i]));
        }
        System.out.println(quotations);
        return quotations;
    }

    //  最高报价价格策略：按照报价单给出的价格由高到低排序
    public static List<Quotation> highPrice(List<Quotation> quotationList) {

        List<Quotation> quotations = new ArrayList<>();

        int size = quotationList.size();

        //  存储下标的数组
        int[] a = new int [size];
        for(int i=0;i<size;i++) {
            a[i] = i;
        }

        //  由大到小排序,记录排序后的下标
        for(int i=0;i<size;i++) {
            for(int j=0;j<size;j++) {
                if(quotationList.get(a[i]).getPrice() < quotationList.get(a[j]).getPrice()) {
                    int k = a[j];
                    a[j] = a[i];
                    a[i] = k;
                }
            }
        }

        //  按照下标重新排列
        for(int i=size-1;i>=0;i--) {
            quotations.add(quotationList.get(a[i]));
        }

        return quotations;
    }

    //  距离最短策略：按照报价门店的距离由近到远排序
    public static List<Quotation> shortDistance(List<Quotation> quotationList, String location) {

        List<Quotation> quotations = new ArrayList<>();

        int size = quotationList.size();

        //  存储下标的数组
        int[] a = new int [size];
        for(int i=0;i<size;i++) {
            a[i] = i;
        }

        //  由小到大排序,记录排序后的下标
        for(int i=0;i<size;i++) {
            for(int j=0;j<size;j++) {

                String location_i = quotationList.get(a[i]).getStore().getLocation();
                String location_j = quotationList.get(a[j]).getStore().getLocation();

                if(getDistance(location, location_i) > getDistance(location, location_j)) {
                    int k = a[j];
                    a[j] = a[i];
                    a[i] = k;
                }
            }
        }

        //  按照下标重新排列
        for(int i=size-1;i>=0;i--) {
            quotations.add(quotationList.get(a[i]));
        }

        return quotations;
    }

    //  品牌一致策略：按照品牌的一致性由高到低排序
    public static List<Quotation> sameBrand(Good good, List<Quotation> quotationList) {
        List<Quotation> quotations = new ArrayList<>();
        Iterator<Quotation> iterator = quotationList.iterator();
        while (iterator.hasNext()){
            Quotation q = iterator.next();
            if (q.getGood().getGoodName().equals(good.getGoodName()) && q.getGood().getGoodType().equals(good.getGoodType()) && q.getGood().getSpec().equals(good.getSpec())){
                quotations.add(q);
                iterator.remove();
            }
        }
        Iterator<Quotation> iterator1 = quotationList.iterator();
        while (iterator1.hasNext()){
            Quotation q = iterator1.next();
            if (q.getGood().getGoodName().equals(good.getGoodName()) || q.getGood().getGoodType().equals(good.getGoodType()) || q.getGood().getSpec().equals(good.getSpec())){
                quotations.add(q);
                iterator1.remove();
            }
        }
        quotations.addAll(quotationList);
        return quotations;
    }

    //  商品保质期策略：按照商品保质期由长到短排序
    public static List<Quotation> longLife(List<Quotation> quotationList) {
        List<Quotation> quotations = new ArrayList<>();
        int size = quotationList.size();
        int[] a = new int [size];
        for(int i=0;i<size;i++) {
            a[i] = i;
        }
        for (int i = 0;i<size;i++){
            for (int j =0; j<size;j++){
                if (quotationList.get(a[i]).getGood().getValidDate().before(quotationList.get(a[j]).getGood().getValidDate())){
                    int k = a[j];
                    a[j] = a[i];
                    a[i] = k;
                }
            }
        }
        for (int i =0;i<size;i++){
            quotations.add(quotationList.get(i));
        }
        return quotations;
    }

}
